package com.xcity.tools.code;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.xcity.game.mail.MailAttach;

import naga.x.platform2.app.Channel;
import naga.x.util.CommonUtils;
import naga.x.util.IOUtils;

/**
 * 渠道礼包码生成工具<br/>
 * 执行后会生成一个sql文件和一个txt文件<br/>
 * 将sql文件导入阿里云平台服务器数据库<br/>
 * 将txt文件发给运营
 * @author yang.li
 *
 */
public class CodeGen {

	// !!!礼包码、sql存放目录!!!
	static File baseDir = new File("D:/temp/libao/渠道/ofc/2017_08_31");//TODO CodeGen
	// !!!礼包类型(唯一)!!!
	static int type = 15;//3;//TODO CodeGen
	// !!!礼包名!!!
	static String name = "官方加群活动礼包";//"4399礼包";//TODO CodeGen
	// !!!礼包渠道!!!
	static Channel channel = Channel.OFFICIAL;//TODO CodeGen
	// !!!礼包限领次数!!!
	static int limit = 1;//TODO CodeGen
	// !!!礼包内容!!!.
	static MailAttach content = new MailAttach();
	static {
		content.addItem(90027, 1);//TODO CodeGen
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
	// !!!礼包码数量!!!
	static int count = 5000;//TODO CodeGen
	
	
	// 礼包码长度(一般不用动)
	static int codeLen = 9;
	// sql文件
	static File sqlFile = new File(baseDir, channel.name + "_code_" + count + "_" + name + ".sql");
	// 礼包码文件
	static File codeFile = new File(baseDir, name + '_' + count + ".txt");
	
	final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
        'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
        'A' , 'B' , 'C' , 'D' , 'E' , 'F' ,
        'G' , 'H' , 'I' , 'J' , 'K' , 'L' ,
        'M' , 'N' , 'O' , 'P' , 'Q' , 'R' ,
        'S' , 'T' , 'U' , 'V' , 'W' , 'X' ,
        'Y' , 'Z'
    };
	
	public static void main(String[] args) {
		
		String sql = "INSERT INTO code_type VALUES({type}, '{name}', '{content}', {limit}, {channel});\n";
		sql = sql.replace("{type}", String.valueOf(type))
				.replace("{name}", name)
				.replace("{content}", JSON.toJSONString(content))
				.replace("{limit}", String.valueOf(limit))
				//TODO CodeGen
				.replace("{channel}", /*"null"*/String.valueOf(channel.ordinal()));
		
		StringBuilder sqlBuf = new StringBuilder(1024);
		StringBuilder codeBuf = new StringBuilder(1024);
		
		sqlBuf.append(sql);
//		System.out.println(sql);
		
		Set<String> codes = new HashSet<>(2024);
		sql = "INSERT INTO code(type, code, state) VALUES({type}, '{code}', 0);\n";
		for (int i = 0; i < count; i++) {
			String code = typeChar(type) + CommonUtils.randomText(codeLen - 1); // 第一位标记类型
			if (codes.add(code)) {
				sqlBuf.append(sql.replace("{type}", String.valueOf(type)).replace("{code}", code));
				codeBuf.append(code).append(System.lineSeparator());
			}
		}
		
		IOUtils.write(codeFile, codeBuf.toString(), IOUtils.UTF8);
		IOUtils.write(sqlFile, sqlBuf.toString(), IOUtils.UTF8);
		System.out.println("ok");
	}
	
	static char typeChar(int type) {
		return digits[type];
	}
	
}
